package PersistanceLayer;

import BusinessLayer.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

import jdbc.ConnectionToOracle;

/**
 * Classe permettant de mapper les objets departement vers la table departement de la base de donnÃ©es.
 * Cette classe herite de la classe DAO.
 * @author Julien CEARD
 * @version 1.0
 */

public class DepartementDAO extends DAO<Departement> {

        // Instanciation d'un objet unique de type DepartementDAO
    private static DepartementDAO instance;

        /**
     * Methode permettant de recuperer l'objet unique de type DepartementDAO
     */
    public final static DepartementDAO getInstance() {
    	if(instance == null)
    	{
    		instance = new DepartementDAO();
    	}
        return instance;
    }
    
    private DepartementDAO()
    {
    	
    }
    
    @Override
    public boolean create(Departement dep) {
        boolean ok = true; //  True si l'insertion s'est dÃ©roulÃ©e correctement, False sinon
        try {
                // Creation et execution de la requete d'ajout d'un nouveau departement
                this.connect.createStatement().executeUpdate(
                            "INSERT INTO Conge " +
                            "(NOMDEPARTEMENT, MNEMO, VERSIONDIPLOME)" +
                            "VALUES ('" + dep.getNomDepartement() + "',"+
                            "'" + dep.getmNemo() + "',"+
                            "'" + dep.getVersionDiplome() + "')");
            }
        catch (SQLException e) {
            // Si exception, on indique que l'insertion ne s'est pas dÃ©roulÃ©e correctement
            ok = false;
        }
        // L'ajout s'est bien derroule si 1 ligne a ete cree dans la base
        return ok;
    }

    @Override
    public boolean update(Departement obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Departement ancien, Departement nouveau) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Departement find(Departement dep) {
        String IDDEP;
        Departement depres = null;
        

        String nomDepartement = "";
        String mNemo = "";
        String verDiplome = "";
        
        ArrayList<Annee> ensAnnees = new ArrayList<Annee>();
        try {


		
			//Cr�ation d'un objet Statement
        	java.sql.Statement state =this.connect.createStatement();
			//L'objet ResultSet contient le r�sultat de la requ�te SQL
			ResultSet result = state.executeQuery("SELECT * FROM DEPARTEMENT where NOMDEPARTEMENT = '" +
					dep.getNomDepartement()+"'");
			//On r�cup�re les MetaData
	

			if(result.next())
			{
				if(result.getObject(3) != null)
					nomDepartement = result.getObject(3).toString();
				
				if(result.getObject(4) != null)
					mNemo = result.getObject(4).toString();
				
				if(result.getObject(5) != null)
					verDiplome = result.getObject(5).toString();
				
				depres = new Departement(nomDepartement, mNemo, verDiplome);
				
				
				if(result.getObject(1) != null)
				{
					result = state.executeQuery("SELECT * FROM ANNEE where IDDEPARTEMENT = '" +
							result.getObject(1)+"'");
					
					while(result.next()){			
						
						if(result.getObject(1) != null)
							ensAnnees.add(new Annee(result.getObject(1).toString(), depres));
					}
					depres.setEnsAnnee(ensAnnees);
					
					
				}

			}

			
		
           result.close();
           state.close();


        } catch (SQLException e) {
                e.printStackTrace();
        }
        return depres;
    }
    
    

    @Override
    public boolean delete(Departement obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Departement> getListe() {
    	
    	String nomDep;
    	String mNemo;
    	String versDiplm;
    	ArrayList<Departement> ensDep = new ArrayList<Departement>();
    	
    	try {
    		java.sql.Statement state =this.connect.createStatement();
    		
    		
			ResultSet res = this.connect.createStatement()
			.executeQuery("SELECT * FROM DEPARTEMENT");
					
			
			while(res.next()){	
				if(res.getObject(3) != null)
					nomDep = res.getObject(3).toString();
				else
					nomDep = "";
				
				if(res.getObject(4) != null)
					mNemo = res.getObject(4).toString();
				else
					mNemo = "";
				
				if(res.getObject(5) != null)
					versDiplm = res.getObject(5).toString();
				else
					versDiplm = "";
				
				ensDep.add(new Departement(nomDep,mNemo,versDiplm));
				
			}
			
			state.close();
			res.close();
			
			return ensDep;
    	} catch (SQLException e) {
			// 
    		
			return null;
		}
    	
    	        
    }


	@Override
	public ArrayList<Departement> getListe(String name) {
		// TODO Auto-generated method stub
		return null;
	}





    
  /*public static void main(String[] args){
		
    	Connection connexion;
    	DepartementDAO departement = new DepartementDAO();
    	ArrayList<Departement> dept = new ArrayList<Departement>();
    	
    	connexion = ConnectionToOracle.getInstance();
    	dept=departement.getListe();
    	System.out.println(dept.toString());
    	}*/


    

	@Override
	public String getNom(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean update(String a, String b, String c, String d) {
		// TODO Auto-generated method stub
		return false;
	}


}
